<template>
  <div class="dashboard-container">
    <!-- 第一行 -->
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="education" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              课程总数
            </div>
            <span class="card-panel-num">{{ dashBoardData.course }}</span>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="guide" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              课程总咨询数
            </div>
            <span class="card-panel-num">{{ dashBoardData.consult }}</span>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="shopping" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              课程总成交数
            </div>
            <span class="card-panel-num">{{ dashBoardData.order }}</span>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="money" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              总收入
            </div>
            <span class="card-panel-num">{{ dashBoardData.money }}</span>
          </div>
        </div>
      </el-col>
    </el-row>


    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div class="chart-container">
      <div id="courseChart" style="height:500px;width:50%;float:left"></div>
      <div id="moneyChart" style="height:500px;width:50%;float:right"></div>
    </div>


  </div>

</template>

<script>
import echarts from 'echarts'
import  'echarts/theme/macarons.js'
import { mapGetters } from 'vuex'
import { getDashBoard,getChart } from '@/api/dashboard'

export default {
  data() {
    return {
      dashBoardData: {},
      courseChart: null,
      moneyChart: null,
      title: '',
      xData: [],
      consultData: [],
      orderData: [],
      moneyData: [],
    }
  },

  methods: {
    getDashBoardData(){
      getDashBoard(this.teacherId).then(result => {
        let { code, data } = result
        if (code === 20000) {
          this.dashBoardData = data.item
        }
      })
    },

    showChart() {
      this.initChartData()
    },

    // 准备图表数据
    initChartData() {
      getChart(this.teacherId).then(result => {
        let { code,data } = result
        if (code === 20000){
          // x轴时间
          this.xData = data.item.dateList
          this.consultData = data.item.consultList
          this.orderData = data.item.orderList
          this.moneyData = data.item.moneyList
          this.setChart()
        }
      })
    },

    // 设置图标参数
    setChart() {
      // 基于准备好的dom，初始化echarts实例
      this.courseChart = echarts.init(document.getElementById('courseChart'),'macarons')
      this.moneyChart = echarts.init(document.getElementById('moneyChart'),'macarons')

      // 指定图表的配置项和数据
      let CourseOption = {
        // x轴是类目轴（离散数据）,必须通过data设置类目数据
        xAxis: {
          type: 'category',
          data: this.xData
        },
        // y轴是数据轴（连续数据）
        yAxis: {
          type: 'value',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          data:['课程成交数','课程咨询数']
        },
        // 系列列表。每个系列通过 type 决定自己的图表类型
        series: [
          {
            name: '课程成交数',
            // 系列中的数据内容数组
            data: this.orderData,
            // 折线图
            type: 'line',
            itemStyle : { normal: {label : {show: true}}}
          },
          {
            name: '课程咨询数',
            // 系列中的数据内容数组
            data: this.consultData,
            // 折线图
            type: 'line',
            itemStyle : { normal: {label : {show: true}}}
          }
        ],
      }

      // 指定图表的配置项和数据
      let moneyOption = {
        // x轴是类目轴（离散数据）,必须通过data设置类目数据
        xAxis: {
          type: 'category',
          data: this.xData
        },
        // y轴是数据轴（连续数据）
        yAxis: {
          type: 'value',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          data:['收入数']
        },
        // 系列列表。每个系列通过 type 决定自己的图表类型
        series: [
          {
            name: '收入数',
            // 系列中的数据内容数组
            data: this.moneyData,
            // 折线图
            type: 'bar',
            itemStyle : { normal: {label : {show: true}}}
          }
        ],
      }

      this.courseChart.setOption(CourseOption)
      this.moneyChart.setOption(moneyOption)
    }
  },

  created() {
    this.getDashBoardData()
  },

  mounted() {
    this.showChart()
  },

  computed: {
    ...mapGetters({
      'teacherId': 'id'
    })
  },


}
</script>

<style lang="scss" scoped>
  .dashboard-container{
    padding: 32px;
    background: #fff;
    position: relative;
  }
  .panel-group {
    margin-top: 10px;

    .card-panel-col {
      margin-bottom: 20px;
    }

    .card-panel {
      height: 108px;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #40c9c6;
        }

        .icon-message {
          background: #36a3f7;
        }

        .icon-money {
          background: #f4516c;
        }

        .icon-shopping {
          background: #34bfa3
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-message {
        color: #36a3f7;
      }

      .icon-money {
        color: #f4516c;
      }

      .icon-shopping {
        color: #34bfa3
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width:550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>
